package com.fengxu.nio.zerocopy;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

// 使用零拷贝技术实现数据复制-服务器端
public class NewIOServer {

    public static void main(String[] args) throws Exception {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.bind(new InetSocketAddress(6666));
        System.out.println("服务器开始监听6666端口...");
        SocketChannel clientChannel = serverSocketChannel.accept();

        ByteBuffer byteBuffer = ByteBuffer.allocate(4096);
        int dataSize = 0;
        while (true){
            int len = clientChannel.read(byteBuffer);
            if(len > 0){
                dataSize += len;
                byteBuffer.rewind();
            } else {
                break;
            }
        }
        System.out.println("传输完毕,共接收到"+dataSize+"字节.");

        serverSocketChannel.close();
    }
}
